package com.xmg.p2p.business.service.impl;

import com.xmg.p2p.base.domain.Account;
import com.xmg.p2p.base.util.BidConst;
import com.xmg.p2p.business.domain.BidRequest;
import com.xmg.p2p.business.domain.MoneyWithdraw;
import com.xmg.p2p.business.domain.SystemAccount;
import com.xmg.p2p.business.domain.SystemAccountFlow;
import com.xmg.p2p.business.mapper.SystemAccountFlowMapper;
import com.xmg.p2p.business.mapper.SystemAccountMapper;
import com.xmg.p2p.business.service.ISystemAccountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * lcd  2020/5/29
 * Description:
 */
@Service
public class SystemAccountServiceImpl implements ISystemAccountService {
    @Autowired
    private SystemAccountMapper systemAccountMapper;

    @Autowired
    private SystemAccountFlowMapper systemAccountFlowMapper;
    @Override
    public void update(SystemAccount systemAccount) {
        int ret = systemAccountMapper.updateByPrimaryKey(systemAccount);
        if (ret == 0) {
            throw new RuntimeException("系统账户对象乐观锁失败");
        }
    }

    @Override
    public void initAccount() {
        SystemAccount current = systemAccountMapper.selectCurrent();
        if (current == null) {
            current = new SystemAccount();
            systemAccountMapper.insert(current);
        }
    }

    @Override
    public void chargeBorrowFee(BidRequest br, BigDecimal manageChargeFee) {
        SystemAccount current = systemAccountMapper.selectCurrent();
        current.setUsableAmount(current.getUsableAmount().add(manageChargeFee));
        systemAccountMapper.updateByPrimaryKey(current);
        //系统流水相关
        SystemAccountFlow systemAccountFlow = new SystemAccountFlow();
        systemAccountFlow.setAccountActionType(BidConst.SYSTEM_ACCOUNT_ACTIONTYPE_MANAGE_CHARGE);
        systemAccountFlow.setAmount(manageChargeFee);
        systemAccountFlow.setCreatedDate(new Date());
        systemAccountFlow.setSystemAccountId(current.getId());
        systemAccountFlow.setNote("系统账户通过这个借款成功"+br.getTitle()+"收取管理费"+manageChargeFee);
        systemAccountFlow.setBalance(current.getUsableAmount());
        systemAccountFlow.setFreezedAmount(current.getFreezedAmount());
        systemAccountFlowMapper.insert(systemAccountFlow);
    }

    @Override
    public void withdrawFee(MoneyWithdraw moneyWithdraw, Account account) {
        SystemAccount current = systemAccountMapper.selectCurrent();
        current.setUsableAmount(current.getUsableAmount().add(moneyWithdraw.getCharge()));
        systemAccountMapper.updateByPrimaryKey(current);
        //系统流水相关
        SystemAccountFlow systemAccountFlow = new SystemAccountFlow();
        systemAccountFlow.setAccountActionType(BidConst.SYSTEM_ACCOUNT_ACTIONTYPE_WITHDRAW_MANAGE_CHARGE);
        systemAccountFlow.setAmount(moneyWithdraw.getCharge());
        systemAccountFlow.setCreatedDate(new Date());
        systemAccountFlow.setSystemAccountId(current.getId());
        systemAccountFlow.setNote("帐号提现成功"+moneyWithdraw.getAccountName()+"帐号"+moneyWithdraw.getAccountNumber()+"收取管理费"+moneyWithdraw.getCharge());
        systemAccountFlow.setBalance(current.getUsableAmount());
        systemAccountFlow.setFreezedAmount(current.getFreezedAmount());
        systemAccountFlowMapper.insert(systemAccountFlow);
    }
}
